package com.hsh.config;

import org.apache.poi.util.DefaultTempFileCreationStrategy;
import org.apache.poi.util.TempFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.File;

@Component
public class ExcelConfig {

    private final static Logger logger = LoggerFactory.getLogger(ExcelConfig.class);

    //    @Value("${application.tmp.path}")
    @Value("${upload.service.path}")
    private String applicationTmpPath;

//    /**
//     * 设置使用SXSSFWorkbook对象导出excel报表时，TempFile使用的临时目录，代替{java.io.tmpdir}
//     */
//    @PostConstruct
//    public void setExcelSXSSFWorkbookTmpPath() {
//        String excelSXSSFWorkbookTmpPath = applicationTmpPath + "/poifiles";
//        File dir = new File(excelSXSSFWorkbookTmpPath);
//        if (!dir.exists()) {
//            dir.mkdirs();
//        }
////		TempFile.setTempFileCreationStrategy(new TempFile.DefaultTempFileCreationStrategy(dir));
//        TempFile.setTempFileCreationStrategy(new DefaultTempFileCreationStrategy(dir));
//        logger.info("setExcelSXSSFWorkbookTmpPath={}", excelSXSSFWorkbookTmpPath);
//    }

    /**
     * 设置使用SXSSFWorkbook对象导出excel报表时，TempFile使用的临时目录，代替{java.io.tmpdir}
     */
    @SuppressWarnings("all")
    @PostConstruct
    public void setExcelSXSSFWorkbookTmpPath() {
        // 临时目录
        String tmpPath = applicationTmpPath + "/poifiles";
        File dir = new File(tmpPath);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        TempFile.setTempFileCreationStrategy(new DefaultTempFileCreationStrategy(dir));
    }
}
